package com.pig4cloud.pig.mt.manage;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.pig4cloud.pig.common.core.util.R;
import com.pig4cloud.pig.common.log.annotation.SysLog;
import com.pig4cloud.pig.mt.entity.WxMsg;
import com.pig4cloud.pig.mt.form.WxMsgReplyForm;
import com.pig4cloud.pig.mt.service.WxMsgReplyService;
import com.pig4cloud.pig.mt.service.WxMsgService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import me.chanjar.weixin.mp.api.WxMpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Map;


/**
 * 微信消息
 */
@RestController
@RequestMapping("/manage/wxMsg")
@Api(value = "/manage/wxMsg", tags = "公众号消息管理")
public class WxMsgManageController {
    @Autowired
    private WxMsgService wxMsgService;
    @Autowired
    private WxMsgReplyService msgReplyService;
    @Autowired
    private WxMpService wxMpService;

    /**
     * 列表
     */
    @ApiOperation(value = "列表", notes = "列表")
    @GetMapping("/list")
	@PreAuthorize("@pms.hasPermission('mt_wxmsg_get')" )
    public R list(@CookieValue String appid, @RequestParam Map<String, Object> params) {
        params.put("appid", appid);
        IPage page = wxMsgService.queryPage(params);

        return R.ok(page);
    }


    /**
     * 信息
     */
    @ApiOperation(value = "信息", notes = "信息")
    @GetMapping("/info/{id}")
	@PreAuthorize("@pms.hasPermission('mt_wxmsg_get')" )
    public R info(@CookieValue String appid, @PathVariable("id") Long id) {
        WxMsg wxMsg = wxMsgService.getById(id);

        return R.ok(wxMsg);
    }

    /**
     * 回复
     */
    @ApiOperation(value = "回复", notes = "回复")
	@SysLog("回复")
    @PostMapping("/reply")
	@PreAuthorize("@pms.hasPermission('mt_wxmsg_add')" )
    public R reply(@CookieValue String appid, @RequestBody WxMsgReplyForm form) {
        msgReplyService.reply(form.getOpenid(), form.getReplyType(), form.getReplyContent());
        return R.ok();
    }

    /**
     * 删除
     */
    @ApiOperation(value = "删除", notes = "删除")
    @PostMapping("/delete")
	@PreAuthorize("@pms.hasPermission('mt_wxmsg_del')" )
    public R delete(@CookieValue String appid, @RequestBody Long[] ids) {
        wxMsgService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
